package com.elinshaw.pattern.per;

import com.elinshaw.BasicThreadPool;
import com.elinshaw.ThreadPool;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Administrator
 */
public class ChatServer {

    private final int port;

    private ThreadPool threadPool;
    private ServerSocket serverSocket;

    public ChatServer(int port) {
        this.port = port;
    }

    public ChatServer() {
        this(1333);
    }

    public void startServer() throws IOException {
        this.threadPool = new BasicThreadPool(1, 4, 2, 1000);
        this.serverSocket = new ServerSocket(port);
        this.serverSocket.setReuseAddress(true);
        for (; ; ) {
            Socket client = serverSocket.accept();
            this.threadPool.execute(new ClientHandler(client));
        }
    }


}
